﻿namespace CrazyTalk.Common
{
    using System;
    using System.Text.RegularExpressions;

    /// <summary>
    /// 类型转换
    /// </summary>
    public class ConvertUtil
    {
        //把字符串转换为指定类型，并返回转换的结果
        public static decimal ToDecimal(string s)
        {
            decimal result;
            decimal.TryParse(s, out result);
            return result;
        }

        /// <summary>
        /// string型转换为bool型
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的bool类型结果</returns>
        public static bool StrToBool(object expression, bool defValue)
        {
            if (expression != null)
                return StrToBool(expression, defValue);

            return defValue;
        }

        /// <summary>
        /// string型转换为bool型
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的bool类型结果</returns>
        public static bool StrToBool(string expression, bool defValue)
        {
            if (expression != null)
            {
                if (String.Compare(expression, "true", StringComparison.OrdinalIgnoreCase) == 0)
                    return true;
                if (String.Compare(expression, "false", StringComparison.OrdinalIgnoreCase) == 0)
                    return false;
            }
            return defValue;
        }

        /// <summary>
        /// 将对象转换为Int32类型
        /// </summary>
        /// <param name="expression"></param>
        /// <returns>转换后的int类型结果</returns>
        public static int ObjectToInt(object expression)
        {
            return ObjectToInt(expression, 0);
        }

        /// <summary>
        /// 将对象转换为Int32类型
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static int ObjectToInt(object expression, int defValue)
        {
            if (expression != null)
                return StrToInt(expression.ToString(), defValue);

            return defValue;
        }

        /// <summary>
        /// 将对象转换为Int32类型,转换失败返回0
        /// </summary>
        /// <param name="str">要转换的字符串</param>
        /// <returns>转换后的int类型结果</returns>
        public static int StrToInt(string str)
        {
            return StrToInt(str, 0);
        }

        /// <summary>
        /// 将对象转换为Int32类型
        /// </summary>
        /// <param name="str">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static int StrToInt(string str, int defValue)
        {
            if (string.IsNullOrEmpty(str) || str.Trim().Length >= 11 || !Regex.IsMatch(str.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$"))
                return defValue;

            int rv;
            if (Int32.TryParse(str, out rv))
                return rv;

            return Convert.ToInt32(StrToFloat(str, defValue));
        }

        public static int ToInt(string s)
        {
            if (string.IsNullOrEmpty(s) || s.Trim().Length >= 11 || !Regex.IsMatch(s.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$"))
                return 0;
            int rv;
            if (Int32.TryParse(s, out rv))
                return rv;
            return Convert.ToInt32(StrToFloat(s, 0));
        }

        /// <summary>
        /// string型转换为float型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static float StrToFloat(object strValue, float defValue)
        {
            if ((strValue == null))
                return defValue;

            return StrToFloat(strValue.ToString(), defValue);
        }

        /// <summary>
        /// string型转换为float型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static float ObjectToFloat(object strValue, float defValue)
        {
            if ((strValue == null))
                return defValue;

            return StrToFloat(strValue.ToString(), defValue);
        }

        /// <summary>
        /// string型转换为float型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <returns>转换后的int类型结果</returns>
        public static float ObjectToFloat(object strValue)
        {
            return ObjectToFloat(strValue.ToString(), 0);
        }

        /// <summary>
        /// string型转换为float型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <returns>转换后的int类型结果</returns>
        public static float StrToFloat(string strValue)
        {
            if ((strValue == null))
                return 0;

            return StrToFloat(strValue, 0);
        }

        /// <summary>
        /// string型转换为float型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static float StrToFloat(string strValue, float defValue)
        {
            if ((strValue == null) || (strValue.Length > 10))
                return defValue;

            float intValue = defValue;
            bool isFloat = Regex.IsMatch(strValue, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
            if (isFloat)
                float.TryParse(strValue, out intValue);

            return intValue;
        }
        /// <summary>
        /// 将对象转换为日期时间类型
        /// </summary>
        /// <param name="str">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static DateTime StrToDateTime(string str, DateTime defValue)
        {
            if (!string.IsNullOrEmpty(str))
            {
                DateTime dateTime;
                if (DateTime.TryParse(str, out dateTime))
                    return dateTime;
            }
            return defValue;
        }

        /// <summary>
        /// 将对象转换为日期时间类型
        /// </summary>
        /// <param name="str">要转换的字符串</param>
        /// <returns>转换后的int类型结果</returns>
        public static DateTime StrToDateTime(string str)
        {
            return StrToDateTime(str, DateTime.Now);
        }

        /// <summary>
        /// 将对象转换为日期时间类型
        /// </summary>
        /// <param name="obj">要转换的对象</param>
        /// <returns>转换后的int类型结果</returns>
        public static DateTime ObjectToDateTime(object obj)
        {
            return StrToDateTime(obj.ToString());
        }

        /// <summary>
        /// 将对象转换为日期时间类型
        /// </summary>
        /// <param name="obj">要转换的对象</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static DateTime ObjectToDateTime(object obj, DateTime defValue)
        {
            return StrToDateTime(obj.ToString(), defValue);
        }

        /// <summary>
        /// 字符串转成整型数组
        /// </summary>
        /// <param name="idList">要转换的字符串</param>
        /// <returns>转换后的int类型结果</returns>
        public static int[] StringToIntArray(string idList)
        {
            return StringToIntArray(idList, -1);
        }

        /// <summary>
        /// 字符串转成整型数组(用,分割)
        /// </summary>
        /// <param name="idList">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static int[] StringToIntArray(string idList, int defValue)
        {
            if (string.IsNullOrEmpty(idList))
                return null;
            string[] strArr = Utils.SplitString(idList, ",");
            int[] intArr = new int[strArr.Length];
            for (int i = 0; i < strArr.Length; i++)
                intArr[i] = StrToInt(strArr[i], defValue);

            return intArr;
        }

        public static DateTime ToDateTime(string s)
        {
            DateTime result;
            DateTime.TryParse(s, out result);
            return result;
        }
        public static bool ToBool(string s)
        {
            bool result;
            bool.TryParse(s, out result);
            return result;
        }
        public static char ToChar(string s)
        {
            char result;
            char.TryParse(s, out result);
            return result;
        }
        public static byte ToByte(string s)
        {
            byte result;
            byte.TryParse(s, out result);
            return result;
        }
        //把对象转换为指定的类型，并返回转换的结果
        public static string ToString(object input)
        {
            try { return Convert.ToString(input); }
            catch { return string.Empty; }
        }
        public static decimal ToDecimal(object input)
        {
            try { return Convert.ToDecimal(input); }
            catch { return 0M; }
        }
        public static float ToFloat(object input)
        {
            try { return float.Parse(input.ToString()); }
            catch { return 0; }
        }
        public static Int64 ToInt64(object input)
        {
            try { return Convert.ToInt64(input); }
            catch { return 0; }
        }
        public static int ToInt(object input)
        {
            try { return Convert.ToInt32(input); }
            catch { return 0; }
        }

        public static DateTime ToDateTime(object input)
        {
            try { return Convert.ToDateTime(input); }
            catch { return new DateTime(1900, 1, 1); }
        }
        public static bool ToBool(object input)
        {
            try { return Convert.ToBoolean(input); }
            catch { return false; }
        }
        public static char ToChar(object input)
        {
            try { return Convert.ToChar(input); }
            catch { return new char(); }
        }
        public static byte ToByte(object input)
        {
            try { return Convert.ToByte(input); }
            catch { return 0; }
        }
        //把对象转换为指定类型，并返回是否成功的结果，输出转换的结果
        public static bool ToString(object input, out string result)
        {
            result = string.Empty;
            try { result = Convert.ToString(input); return true; }
            catch { return false; }
        }
        public static bool ToDecimal(object input, out decimal result)
        {
            result = 0M;
            try
            {
                result = Convert.ToDecimal(input);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool ToInt(object input, out int result)
        {
            result = 0;
            try
            {
                result = Convert.ToInt32(input);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool ToDateTime(object input, out DateTime result)
        {
            result = new DateTime(1900, 1, 1);
            try
            {
                result = Convert.ToDateTime(input);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool ToBool(object input, out bool result)
        {
            result = false;
            try
            {
                result = Convert.ToBoolean(input);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool ToChar(object input, out char result)
        {
            result = new char();
            try
            {
                result = Convert.ToChar(input);
                return true;
            }
            catch { return false; }
        }
        public static bool ToByte(object input, out byte result)
        {
            result = 0;
            try { result = Convert.ToByte(input); return true; }
            catch { return false; }
        }


        public static string ToBase64(string str)
        {
            if (string.IsNullOrEmpty(str)) return str;
            try
            {
                byte[] b = System.Text.Encoding.Default.GetBytes(str);
                return Convert.ToBase64String(b);
            }
            catch { return str; }
        }

        public static string FromBase64(string base64Str)
        {
            if (string.IsNullOrEmpty(base64Str)) return "";
            try
            {

                byte[] b = Convert.FromBase64String(base64Str);
                return System.Text.Encoding.Default.GetString(b);
            }
            catch { return ""; }
        }
    }
}

